# -*- coding: utf-8 -*-
"""
Created on Fri Jan  5 12:24:47 2018

@author: Luther
"""
import os
import chardet


def returncut(cutl, text):
    s = -1
    cnt = 0
    while s == -1:
        try:
            s = text.find(cutl[cnt])
        except IndexError:
            raise ValueError
        cnt = cnt + 1
    return s


def returnmincut(cutl, text):
    cl = []
    for i in cutl:
        s = text.find(i)
        if s != -1:
            cl.append(s)
    return min(cl)


def readtext(path):
    try:
        with open(path) as f:
            text = f.read()
            text = "".join(text.split())
    except UnicodeDecodeError:
        with open(path, encoding="utf-8") as f:
            text = f.read()
            text = "".join(text.split())
    return text


def readunknowntext(path):
    with open(path, 'rb') as f:
        data = f.read()
        ret = chardet.detect(data)
        with open(path, encoding=ret['encoding'], errors='ignore') as f2:
            text = f2.read()
            text = "".join(text.split())
    return text


def main(path):

    file_list = [
        x for x in os.listdir(path) if os.path.splitext(x)[-1] == '.txt'
    ]
    print("{:}个待检索文件读取成功!".format(len(file_list)))

    count = 0
    for each in file_list:

        file_path = os.path.join(path, each)
        year = eval(each[:4])
        text = ""

        try:
            text = readtext(file_path)
        except UnicodeDecodeError:
            try:
                text = readunknowntext(file_path)
            except UnicodeDecodeError:
                pass

        try:

            # cut catalog:
            cutl = ["注册地址", "办公地址", "法定代表人"]
            s = returnmincut(cutl, text)
            text = text[s:]

            # cut start:
            if year < 2017:
                cutl = ["展望", "未来发展"]
                s = returncut(cutl, text)
            else:
                cutl = ["未来发展的讨论与分析", "未来发展的展望", "展望"]
                s = returncut(cutl, text)
            text = text[s:]

            # cut end:
            if year < 2012:
                cutl = ["投资情况", " 利润分配", "募集资金", "重要事项"]
                s = returnmincut(cutl, text)
                text = text[:s]
            elif year < 2015:
                cutl = ["非标准审计报告", "利润分配", "募集资金", "重要事项"]
                s = returnmincut(cutl, text)
            else:
                cutl = ["接待调研", "重要事项"]
                s = returnmincut(cutl, text)
            text = text[:s]

        except ValueError:
            text = ""

        if text != "":
            try:
                os.mkdir(os.path.join(path, "ret"))
            except FileExistsError:
                pass
        result_path = os.path.join(path, "ret", each)
        with open(result_path, "w", encoding='utf-8') as f:
            f.write(text)
        count = count + 1
        print("\r当前进度: {:.2f}%".format(count * 100 / len(file_list)), end="")


if __name__ == '__main__':
    path = input("请输入待检索文件目录：")
    main(path)
